import os

from fastapi import FastAPI, UploadFile, File

from server import success
from util.pdf import parse_pdf_pages

UPLOAD_DIR = '/tmp'
DEFAULT_TBPU = "single_para"
app = FastAPI()

@app.post("/pdf/pages")
async def parse_pages(file: UploadFile = File(...)):
    local_file = os.path.join(UPLOAD_DIR, file.filename)
    with open(local_file, "wb") as f:
        content = await file.read()
        f.write(content)
    pages = parse_pdf_pages(local_file, DEFAULT_TBPU)
    return success(pages)

if __name__ == "__main__":
    # 监听特定的IP地址，例如192.168.1.100
    import uvicorn
    uvicorn.run(app="pdftext:app", host="0.0.0.0", port=8001, workers=1)
